#!/usr/bin/env python
# Wrapper to calculate IO
import sys
sys.path.append('/opt/ssp_plugin/plib')
import libsql as sql
import networkx as nx
import gimple2graph as g2g

database_name = sys.argv[1]
Sql = sql.SQL(database_name, None)
err = 0
err1 = 0
err2 = 0
err3 = 0
sum = 0
sum1 = 0
sum2 = 0
sum3 = 0

for t in Sql.tls():
    tn = "`" + t[0] + "`"
    print "\n\nAnalysis functin %s from database %s ..." % (database_name, tn)
    instance = sql.SQL(database_name, tn)
    G = g2g.GRAPH(t[0])
    G.get(instance)
    G.lsp()
    #G.work()
    G.fw()
    G.eval()
    err = err + G.err
    err1 = err1 + G.err1
    err2 = err2 + G.err2
    err3 = err3 + G.err3
    sum = sum + G.sum
    sum1 = sum1 + G.sum1
    sum2 = sum2 + G.sum2
    sum3 = sum3 + G.sum3
    #except nx.exception.NetworkXError:
    #except test:
    #    print "ignore " + tn
    #G.draw()
    instance.free()
Sql.free()
print "[ERROR REPORT]", err1, err2, err3, err
print "[SUM REPORT]", sum1, sum2, sum3, sum
print "[I Error Rate]", float(err1)/sum1
print "[O Error Rate]", float(err2)/sum2
print "[P Error Rate]", float(err3)/sum3
print "[  Error Rate]", float(err)/sum
